package com.xiangban.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;

import static com.xiangban.utils.RedisUtil.fex_open_id_session_key;
import static com.xiangban.utils.RedisUtil.fex_session_key;

@Slf4j
@Component
public class CommonUtil {
    public static final String common_1="1";
    public static final String common_2="2";
    public static final String common_3="3";
    public static final String common_4="4";
    public static final String common_5="5";
    public static final String common_6="6";
    public final static String token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    @Autowired
    private RedisUtil redisUtil;




    public static String getToken(){
        return UUID.randomUUID().toString().replace("-", "");
    }
    public static boolean BizEmpty(Object o){

        return o ==null || o.toString().length()<1;

    }
    public static String MD5(String data){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(data.getBytes("UTF-8"));
            StringBuilder stringBuilder = new StringBuilder();
            for (byte item: array) {
                stringBuilder.append(Integer.toHexString((item & 0xFF)| 0x100).substring(1,3));
            }
            return stringBuilder.toString().toUpperCase();
        }catch (Exception exception){
        }
        return null;
    }

    /**
     * 失效验证码
     *
     * @param type  验证码类型
     * @param phone 用户手机号
     */
    public  void invalidVerification(String type, String phone) {
        redisUtil.del(type + phone);
    }

    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     *
     * @param str1 字符串 1
     * @param str2 字符串 2
     * @return boolean
     */
    public static boolean compare(String str1, String str2) {
        return str1.equals(str2);
    }

    /**
     * 保存用户sessionKey 对应openId
     * @param openid openId
     * @param sessionKey sessionKey
     */
    public void sessionKey(String openid , String sessionKey){
        //删除上次sessionKey
        String oldSessionKey = redisUtil.get(fex_open_id_session_key + openid);
        redisUtil.del(fex_session_key+oldSessionKey);
        redisUtil.set(fex_open_id_session_key+openid, sessionKey, RedisUtil.session_key_time);
        redisUtil.set(fex_session_key+sessionKey, openid, RedisUtil.session_key_time);

    }

    /**
     * 根据sessionKey 获取openId
     * @param sessionKey  sessionKey
     * @return
     */

    public String getOpenId(String sessionKey){

        return redisUtil.get(fex_session_key+sessionKey);

    }

    /**
     * 获取客户端ip
     * @return
     */
    public static String getHostIp(){
        String ip = "";
        try{
            ip = InetAddress.getLocalHost().getHostAddress();
        }catch(UnknownHostException e){
            e.printStackTrace();
        }
        return ip;
    }




}
